<?php
session_start();
error_reporting(E_ALL & ~E_DEPRECATED);
require_once("../db_connect.php");

	$volunteer_information_id = "";

	$sql = "SELECT max(volunteer_information_id) as volunteer_information_id  FROM volunteer_information";
	$sth = $db->query($sql);
	if (DB::isError($sth)) {
	  exit($sth->getMessage());
	}

	while ($data = $sth->fetchRow(DB_FETCHMODE_ASSOC)) {
	   $volunteer_information_id = $data['volunteer_information_id'];
	}
	$volunteer_information_id++;
	echo $volunteer_information_id;
	$user_id  = htmlspecialchars($_SESSION['user_id'],ENT_QUOTES,"UTF-8");

	$volunteer_Name  = htmlspecialchars($_POST['volunteer_Name'],ENT_QUOTES,"UTF-8");
	$sub_category_id  = htmlspecialchars($_POST['sub_category_id'],ENT_QUOTES,"UTF-8");
	$content   = htmlspecialchars($_POST['content'],ENT_QUOTES,"UTF-8");
	$age   = htmlspecialchars($_POST['age'],ENT_QUOTES,"UTF-8");
	$Target_Gender   = htmlspecialchars($_POST['Target_Gender'],ENT_QUOTES,"UTF-8");

	$work_time    = htmlspecialchars($_POST['work_time'],ENT_QUOTES,"UTF-8");
	$Location     = htmlspecialchars($_POST['Location'],ENT_QUOTES,"UTF-8");
	$access     = htmlspecialchars($_POST['access'],ENT_QUOTES,"UTF-8");
	$Conditions   = htmlspecialchars($_POST['Conditions'],ENT_QUOTES,"UTF-8");
	$pref   = htmlspecialchars($_POST['pref'],ENT_QUOTES,"UTF-8");

	$Activity_Date   = date("Y-m-d",mktime(0,0,0,$_POST['Activity_Date_month'],$_POST['Activity_Date_day'],$_POST['Activity_Date_year']));
	$End_time     = date("Y-m-d",mktime(0,0,0,$_POST['End_time_month'],$_POST['End_time_day'],$_POST['End_time_year']));

	$application_number       = htmlspecialchars($_POST['application_number'],ENT_QUOTES,"UTF-8");
	$other  = htmlspecialchars($_POST['other'],ENT_QUOTES,"UTF-8");

	$treatment  = $_POST['treatment_id'];
	for($i=0;$i<count($treatment);$i++){
		$sql = "INSERT INTO volunteer_information(volunteer_information_id,user_id,volunteer_Name,content,sub_category_id,age,Target_Gender,work_time,Location,access,Conditions,Activity_Date,End_time,application_number,treatment_id,other,pref)
			VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		$stmt = $db->prepare($sql);
    	$data = array($volunteer_information_id,
    			  $user_id,
    			  $volunteer_Name,
                  $content,
                  $sub_category_id,
                  $age,
                  $Target_Gender,
                  $work_time,
                  $Location,
                  $access,
                  $Conditions,
                  $Activity_Date,
                  $End_time,
                  $application_number,
                  htmlspecialchars($treatment[$i],ENT_QUOTES,"UTF-8"),
                  $other,
                  $pref);
	$db->execute($stmt, $data);     //UPDATE文を実行
	if (PEAR::isError($db)) {       //SQLの実行に失敗すればここでエラー文を出力
    	die($res->getMessage());
	}
	}

	header("Location: ../user_function/user_display_volunteer.php");
?>

